import numpy as np
import math

class PlaneTransformCalcer:
    def __init__(self):
        self.source_list = []
        self.dest_list   = []

    def append_source_point(self, x, y):
        self.source_list.append((x,y))
    
    def append_dest_point(self, x, y):
        self.dest_list.append((x,y))

    def get_rotate_and_offset(self):
        if len(self.source_list) != 3 or len(self.dest_list) != 3:
            raise ValueError("给定的点数不够")

        # 构造矩阵A
        A = np.zeros((6, 6))
        i = 0
        for x,y in self.source_list:
            A[i, 0:3] = [x, y, 1]
            A[i+1, -3:None] = [x, y, 1]

            i = i + 2

        # 构造右侧向量b
        b = np.zeros(6)
        i = 0
        for x,y in self.dest_list:
            b[i:i+2] = [x, y]
            i = i + 2

        # 求解方程
        x = np.linalg.solve(A, b)

        return ((180/math.pi)*(math.acos(x[0])), x[2], x[5])

    def get_rotate_and_offset_result(self, sx, sy, zeta, tx, ty):
        _zeta = zeta * (math.pi/180.0)
        return (math.cos(_zeta)*sx - math.sin(_zeta)*sy + tx, math.sin(_zeta)*sx + math.cos(_zeta)*sy + ty)

class ParamLine:
    def __init__(self, x1, y1, x2, y2):
        if y1 == y2:# 和x轴平行
            self.A = 0
            self.B = 1
            self.C = -y1
        elif x1 == x2:# 和y轴平行
            self.A = 1
            self.B = 0
            self.C = -x1
        else:
            self.A = -(y2-y1)/(x2-x1)
            self.B = 1
            self.C = -self.A * x1 - y1

    def calc_intersection(self, other_line):
        A1 = self.A
        B1 = self.B
        C1 = self.C

        A2 = other_line.A
        B2 = other_line.B
        C2 = other_line.C

        return {'y': (-A1*C2 + A2*C1)/(A1*B2 - A2*B1), 'x': (B1*C2 - B2*C1)/(A1*B2 - A2*B1)}

    def calc_intersection_func(self, X_1, Y_1, X_2, Y_2, X_3, Y_3, X_4, Y_4):
        # 计算A1,B1,C1
        if Y_1 == Y_2:# 和x轴平行
            A1 = 0
            B1 = 1
            C1 = -Y_1
        elif X_1 == X_2:# 和y轴平行
            A1 = 1
            B1 = 0
            C1 = -X_1
        else:
            A1 = -(Y_2-Y_1)/(X_2-X_1)
            B1 = 1
            C1 = -A1 * X_1 - Y_1

        # 计算A2,B2,C2
        if Y_3 == Y_4:# 和x轴平行
            A2 = 0
            B2 = 1
            C2 = -Y_3
        elif X_3 == X_4:# 和y轴平行
            A2 = 1
            B2 = 0
            C2 = -X_3
        else:
            A2 = -(Y_4-Y_3)/(X_4-X_3)
            B2 = 1
            C2 = -A2 * X_3 - Y_3
        
        return {'y': (-A1*C2 + A2*C1)/(A1*B2 - A2*B1), 'x': (B1*C2 - B2*C1)/(A1*B2 - A2*B1)}

if __name__ == "__main__":
    # PTC = PlaneTransformCalcer()
    # PTC.append_source_point(0,0)
    # PTC.append_source_point(1,0)
    # PTC.append_source_point(2,1)
    # d1 = PTC.get_rotate_and_offset_result(0,0, 60, 1,2)
    # PTC.append_dest_point(d1[0], d1[1])
    #
    # d2 = PTC.get_rotate_and_offset_result(1,0, 60, 1,2)
    # PTC.append_dest_point(d2[0], d2[1])
    #
    # d3 = PTC.get_rotate_and_offset_result(2,1, 60, 1,2)
    # PTC.append_dest_point(d3[0], d3[1])
    #
    # print(PTC.get_rotate_and_offset())

    l1 = ParamLine(1, 1, 2, 2)

    print(l1.calc_intersection_func(-15.627,76.25, 21.878, 76.481, 36.75, 31.775, 36.122, -29.171))
